package com.springcloud.sc.ucpv5.dushanzi.config.util;

import com.springcloud.sc.ucpv5.client.commondata.MyGroup;
import com.springcloud.sc.ucpv5.client.commondata.MyUser;
import com.springcloud.sc.ucpv5.client.module.datamanager.BaseDataManager;

import java.math.BigInteger;
import java.util.List;

public class DuShanZiUtil {
    public static String 根据父组号码获取子组号码(MyGroup parent) {
        String groupNumber_new = null;
        try {
            // 根据父组, 构建子组号码
            // 父组是否以有以0结尾
            String number = parent.getTelno();
            String number1 = number;
            while (true) {
                String tmp = number1.substring(number1.length() - 1);
                if (!tmp.equals("0")) {
                    break;
                }
                number1 = number1.substring(0, number1.length() - 1);
            }

            if (number1.length() == number.length()) {
                // 不是以0结尾的, 则在这个基础上加1
            } else if (number.length() - number1.length() >= 2) {
                for (int i = 1; i < 100; i++) {
                    String number_before = number1 + (i < 10 ? "0" + i : i);
                    while (number_before.length() < number.length()) {
                        number_before = number_before + "0";
                    }
                    if (根据父组号码获取子组号码判断组是否存在(number_before.toString())) {
                        groupNumber_new = number_before.toString();
                        break;
                    }
                }
            } else if (number.length() - number1.length() == 1) {
                for (int i = 1; i < 10; i++) {
                    String number_before = number1 + i;
                    while (number_before.length() < number.length()) {
                        number_before = number_before + "0";
                    }
                    if (根据父组号码获取子组号码判断组是否存在(number_before.toString())) {
                        groupNumber_new = number_before.toString();
                        break;
                    }
                }
            }

            if (groupNumber_new == null) {
                // 大整数加一, 来遍历出来一个
                try {
                    BigInteger number_before = new BigInteger(number);

                    BigInteger skin = new BigInteger("1");
                    while (true) {
                        number_before = number_before.add(skin);
                        if (根据父组号码获取子组号码判断组是否存在(number_before.toString())) {
                            groupNumber_new = number_before.toString();
                            break;
                        }
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }

        } catch (Exception e) {

        }
        return groupNumber_new;
    }

    public static boolean 根据父组号码获取子组号码判断组是否存在(String number) {
        // 查看该组是否存在
        MyGroup group = BaseDataManager.getInstance().groupGet(number);

        // 查看是否有该用户(以防万一)
        MyUser user = BaseDataManager.getInstance().userGet(number);

        if (user == null && group == null) {
            return true;
        }
        return false;
    }

    public static String 检查用户在应急组中是否存在组编号冲突(String group, List<String> voiceAuthorityGroupList) {
        StringBuilder builder = new StringBuilder();
        MyGroup group_input = BaseDataManager.getInstance().groupGet(group);
        String index = group_input.getIdlData().keep1;

        if(index.equalsIgnoreCase("")) {
            return "";
        }

        for(String group_ : voiceAuthorityGroupList) {
            try {
                MyGroup group_keep = BaseDataManager.getInstance().groupGet(group_);
                String index_keep = group_keep.getIdlData().keep1;
                if (!index_keep.equalsIgnoreCase("") && index.equals(index_keep) && !group_input.equals(group_keep)) {
                    builder.append("设置发布权限存在组编号冲突, 组编号的设置都为");
                    builder.append(index);
                    builder.append(":");
                    builder.append(group_input.getIdlData().GroupName);
                    builder.append(",");
                    builder.append(group_keep.getIdlData().GroupName);

                    return builder.toString();
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        return builder.toString();

    }
}
